package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Bod;
import model.Veiling;

import database.ReadDAO;

/**
 * Servlet implementation class refreshBids
 */
public class refreshBids extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PrintWriter out;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public refreshBids() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();
		ReadDAO read = new ReadDAO();
		try{
			int veilingid = Integer.parseInt(request.getParameter("veilingid"));
			Veiling veiling = read.getVeiling(veilingid);
			ArrayList<Bod> biedingen = read.getBiedingen(veilingid);
			if(veiling != null){
				if(request.getParameter("action").equals("getRemaining")){
					out.println("<h6>latest bids</h6>");
					out.println("<ul id=\"naam\">");
					if(biedingen.size() == 0){
						out.println("<li>Minimum price:</li>");
					}else{
						out.println("<b><li>Name</li></b>");
						for(Bod b : biedingen){
							out.println("<li>"+b.getBieder().getVoornaam()+"</li>");
						}
					}
					out.println("</ul>");
					out.println("<ul id=\"prijs\">");
					if(biedingen.size() == 0){
						out.println("<li>"+veiling.getHighestBid()+"</li>");
					}else{
						out.println("<b><li>Price</li></b>");
						for(Bod b : biedingen){
							out.println("<li>"+b.getPrijs()+"</li>");
						}
					}
					out.println("</ul>");
					out.println("<ul id=\"datum\">");
					if(biedingen.size() == 0){
						out.println("<li></li>");
					}else{
						out.println("<b><li>Date</li></b>");
						for(Bod b : biedingen){
							out.println("<li>"+b.getDatum()+"</li>");
						}
					}
					out.println("</ul>");
				}
			}
		}catch(NumberFormatException e){
			//do nothing
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
